]> git.saurik.com Git - apple/libdispatch.git/blob - examples/Dispatch Samples/apply.c
libdispatch-84.5.1.tar.gz
[apple/libdispatch.git] / examples / Dispatch Samples / apply.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_DTS_LICENSE_HEADER_START@
5 *
6 * IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
7 * ("Apple") in consideration of your agreement to the following terms, and your
8 * use, installation, modification or redistribution of this Apple software
9 * constitutes acceptance of these terms. If you do not agree with these terms,
10 * please do not use, install, modify or redistribute this Apple software.
11 *
12 * In consideration of your agreement to abide by the following terms, and
13 * subject to these terms, Apple grants you a personal, non-exclusive license,
14 * under Apple's copyrights in this original Apple software (the "Apple Software"),
15 * to use, reproduce, modify and redistribute the Apple Software, with or without
16 * modifications, in source and/or binary forms; provided that if you redistribute
17 * the Apple Software in its entirety and without modifications, you must retain
18 * this notice and the following text and disclaimers in all such redistributions
19 * of the Apple Software. Neither the name, trademarks, service marks or logos of
20 * Apple Computer, Inc. may be used to endorse or promote products derived from
21 * the Apple Software without specific prior written permission from Apple. Except
22 * as expressly stated in this notice, no other rights or licenses, express or
23 * implied, are granted by Apple herein, including but not limited to any patent
24 * rights that may be infringed by your derivative works or by other works in
25 * which the Apple Software may be incorporated.
26 *
27 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
28 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
29 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
31 * COMBINATION WITH YOUR PRODUCTS.
32 *
33 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
35 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
37 * DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
38 * CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
39 * APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * @APPLE_DTS_LICENSE_HEADER_END@
42 */
43
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <sys/errno.h>
48 #include <assert.h>
49 #include <dispatch/dispatch.h>
50 #include <mach/mach_time.h>
51
52 #define kIT 10
53
54 uint64_t elapsed_time;
55
56 void timer_start() {
57 elapsed_time = mach_absolute_time();
58 }
59
60 double timer_milePost() {
61 static dispatch_once_t justOnce;
62 static double scale;
63
64 dispatch_once(&justOnce, ^{
65 mach_timebase_info_data_t tbi;
66 mach_timebase_info(&tbi);
67 scale = tbi.numer;
68 scale = scale/tbi.denom;
69 printf("Scale is %10.4f Just computed once courtesy of dispatch_once()\n", scale);
70 });
71
72 uint64_t now = mach_absolute_time()-elapsed_time;
73 double fTotalT = now;
74 fTotalT = fTotalT * scale; // convert this to nanoseconds...
75 fTotalT = fTotalT / 1000000000.0;
76 return fTotalT;
77 }
78
79 int
80 main(void)
81 {
82 dispatch_queue_t myQueue = dispatch_queue_create("myQueue", NULL);
83 dispatch_group_t myGroup = dispatch_group_create();
84
85 // dispatch_apply on a serial queue finishes each block in order so the following code will take a little more than a second
86 timer_start();
87 dispatch_apply(kIT, myQueue, ^(size_t current){
88 printf("Block #%ld of %d is being run\n",
89 current+1, // adjusting the zero based current iteration we get passed in
90 kIT);
91 usleep(USEC_PER_SEC/10);
92 });
93 printf("and dispatch_apply( serial queue ) returned after %10.4lf seconds\n",timer_milePost());
94
95 // dispatch_apply on a concurrent queue returns after all blocks are finished, however it can execute them concurrently with each other
96 // so this will take quite a bit less time
97 timer_start();
98 dispatch_apply(kIT, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t current){
99 printf("Block #%ld of %d is being run\n",current+1, kIT);
100 usleep(USEC_PER_SEC/10);
101 });
102 printf("and dispatch_apply( concurrent queue) returned after %10.4lf seconds\n",timer_milePost());
103
104 // To execute all blocks in a dispatch_apply asynchonously, you will need to perform the dispatch_apply
105 // asynchonously, like this (NOTE the nested dispatch_apply inside of the async block.)
106 // Also note the use of the dispatch_group so that we can ultimatly know when the work is
107 // all completed
108
109 timer_start();
110 dispatch_group_async(myGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
111 dispatch_apply(kIT, myQueue, ^(size_t current){
112 printf("Block #%ld of %d is being run\n",current+1, kIT);
113 usleep(USEC_PER_SEC/10);
114 });
115 });
116
117 printf("and dispatch_group_async( dispatch_apply( )) returned after %10.4lf seconds\n",timer_milePost());
118 printf("Now to wait for the dispatch group to finish...\n");
119 dispatch_group_wait(myGroup, UINT64_MAX);
120 printf("and we are done with dispatch_group_async( dispatch_apply( )) after %10.4lf seconds\n",timer_milePost());
121 return 0;
122 }
123